package com.example.hasee.zhcd.Activities.QianDao;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.hasee.zhcd.Activities.WelcomeActivity;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2018/6/4.
 */

public class TagActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private Spinner CL_spinner;
    private Button btn_dao;
    private ArrayList<String> cl_name = new ArrayList<String>();
    private ArrayList<String> cl_code = new ArrayList<String>();
    private String strAccount;
    private String strCL;
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private ArrayAdapter<String> adapter;

    @Override
    public int contentViewId() {
        return R.layout.activity_dao;
    }

    @Override
    public void set() {
        title.setText("签到");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        btn_dao.setOnClickListener(this);
        getCLs();
    }

    @Override
    public void init() {
        strAccount = loginmap.get("account");
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        CL_spinner = (Spinner) findViewById(R.id.cl);
        btn_dao = (Button) findViewById(R.id.btn_dao);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZH_Signin_CL":
                ProgressDialogUtils.closeProgressDialog();
                //E/ZH_Signin_CL: {"data": [{"fcode":"1", "fname":"冀C.0E070"},{"fcode":"2", "fname":"冀C.0E071"},{"fcode":"3", "fname":"冀C.0N070"},
                // {"fcode":"4", "fname":"冀C.CW651"},{"fcode":"5", "fname":"冀C.ZH652"}],"rspcode": 0,"rspmessage":""}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            cl_name.add(data.getJSONObject(i).getString("fname"));
                            cl_code.add(data.getJSONObject(i).getString("fcode"));
                        }
                        adapter = new ArrayAdapter<String>(TagActivity.this, android.R.layout.simple_spinner_dropdown_item, cl_name);
                        CL_spinner.setAdapter(adapter);
                        CL_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                strCL = cl_code.get(i);
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZH_Signin":
                ProgressDialogUtils.closeProgressDialog();
//                Log.e("ZH_Signin", msg.getMsg());
                //ZH_Signin: {"data":"","rspcode":3,"rspmessage":"已签到！"}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    FinishDialog(rspmessage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getCLs() {
        ProgressDialogUtils.createProgressDialog(TagActivity.this, "正在获取车辆信息...");
        HashMap<String, String> map = new HashMap<String, String>();
        httpUtils.get("ZH_Signin_CL", map, "ZH_Signin_CL");
    }

    private void getTag() {
        ProgressDialogUtils.createProgressDialog(TagActivity.this, "正在签到...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strAccount", strAccount);
        map.put("strCL", strCL);
        httpUtils.get("ZH_Signin", map, "ZH_Signin");
    }

    private void FinishDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(TagActivity.this);
        builder.setMessage(message);
        builder.setTitle("提示");
        builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                arg0.dismiss();
                TagActivity.this.finish();
            }
        });
        builder.create().setCanceledOnTouchOutside(false);
        builder.setCancelable(false);
        builder.show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_dao:
                getTag();
                break;
        }
    }
}
